package com.zengshuai.miniprogrambackend.Controller;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.zengshuai.miniprogrambackend.Common.constant.MyOss;
import com.zengshuai.miniprogrambackend.Common.constant.OssClient;
import com.zengshuai.miniprogrambackend.Entity.*;
import com.zengshuai.miniprogrambackend.Mapper.OrderMapper;
import com.zengshuai.miniprogrambackend.Mapper.ServiceMapper;
import com.zengshuai.miniprogrambackend.Mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/backend")
public class ManageSystemController {

    @Autowired
    UserMapper userMapper;
    @Autowired
    ServiceMapper serviceMapper;

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    MyOss oss;
    @Autowired
    OssClient ossClient;


    @GetMapping("/login")
    public Integer login(String username, String password) {

        return userMapper.login(username, password);


    }


    //获取一般用户
    @GetMapping("/getuserlist")
    public List<User> getuserlist(Integer page, Integer pagesize) {
        System.out.println(page);
        System.out.println(pagesize);
        Map<String, Integer> map = new HashMap<>();
        map.put("page", page);
        map.put("pagesize", pagesize);
        System.out.println(userMapper.getUserlistByLimit(map));
        return userMapper.getUserlistByLimit(map);
    }

    //获取查询数据总长度
    @GetMapping("/getuserlistlength")
    public Integer getuserlistlength() {

        return userMapper.getuserlistlength();

    }

    //查询用户by openid
    @GetMapping("/cxuserbyopenid")
    public List<User> getuserlist(Integer page, Integer pagesize, String openid) {

        System.out.println(userMapper.cxuserbyopenid(page, pagesize, openid));
        return userMapper.cxuserbyopenid(page, pagesize, openid);

    }

    //获取员工
    @GetMapping("/getemployeelist")
    public List<Employee> getuserlist2(Integer page, Integer pagesize) {
        System.out.println(page);
        System.out.println(pagesize);
        Map<String, Integer> map = new HashMap<>();
        map.put("page", page);
        map.put("pagesize", pagesize);
//        System.out.println(userMapper.getUserlistByLimit2(map));
        return userMapper.getUserlistByLimit2(map);
    }

    //获取长度
    @GetMapping("/getemployeelistlength")
    public Integer getuserlistlength2() {

        return userMapper.getuserlistlength2();

    }

    //添加员工

    @GetMapping("/addemployee")
    public Integer addemployee(String employeeid, String password, String telnumber, String name) {


        if (userMapper.employeeidisExisted(employeeid) == 0) {
            userMapper.addemployee(employeeid, password, telnumber, name);
            return 1;

        } else {
            return 0;
        }


    }

    //查询
    @GetMapping("/cxemployeebyemployeeid")
    public List<Employee> cxemployeebyemployeeid(Integer page, Integer pagesize, String employeeid) {


        System.out.println(userMapper.cxemployeebyemployeeid(page, pagesize, employeeid));
        return userMapper.cxemployeebyemployeeid(page, pagesize, employeeid);

    }


    //删除员工
    @GetMapping("/deleteEmployee/{id}")
    public void deleteEmployee(@PathVariable("id") String id) {

//        System.out.println(id);

        userMapper.deleteEmployee(id);

    }

    //修改员工
    @GetMapping("/updateemployee")
    public int updateemployee(String employeeid, String password, String telnumber, String name, String id) {

//        System.out.println(userMapper.employeeidisExisted(employeeid));
        if (userMapper.employeeidisExisted(employeeid) == 0) {
            userMapper.updateEmployee(employeeid, password, telnumber, name, id);
            return userMapper.updateEmployee(employeeid, password, telnumber, name, id);
        } else {
            return 0;
        }

    }


    //获取服务列表
    @GetMapping("/getservicelist")
    public List<Service> getservicelist(Integer page, Integer pagesize) {


        System.out.println(serviceMapper.getservicelist(page, pagesize));
        return serviceMapper.getservicelist(page, pagesize);

    }

    //获取数据长度
    @GetMapping("/getservicelistlength")
    public Integer getservicelistlength() {

        return serviceMapper.getservicelistlength();

    }


    //模糊查询
    @GetMapping("/cxServiceListByServiceName")
    public List<Service> cxServiceListByServiceName(Integer page, Integer pagesize, String servicename) {

        //        System.out.println(servicename);
        return serviceMapper.cxServiceListByServiceName(page, pagesize, servicename);

    }

    //修改服务

    @GetMapping("/updateserveice")
    public int updateserveice(String servicename, String servicecategory, String serviceprice, String description, String serviceintroduction, String serviceid, String oldservicename) {


        if (serviceMapper.servicenameisExisted(servicename) == 0) {
            serviceMapper.updateService(servicename, servicecategory, serviceprice, description, serviceintroduction, serviceid);
            return serviceMapper.updateService(servicename, servicecategory, serviceprice, description, serviceintroduction, serviceid);
        }

        if (oldservicename.equals(servicename)) {
            System.out.println("ssssss");
            serviceMapper.updateService(servicename, servicecategory, serviceprice, description, serviceintroduction, serviceid);
            return serviceMapper.updateService(servicename, servicecategory, serviceprice, description, serviceintroduction, serviceid);
        } else {
            return 0;
        }

    }


    //添加服务
    @GetMapping("/addService")
    public void addService(String servicename, String servicecategory, String serviceprice, String description, String serviceintroduction) {


        serviceMapper.addService(servicename, servicecategory, serviceprice, description, serviceintroduction);

    }

    //删除服务
    @GetMapping("/deleteService/{id}")
    public void deleteService(@PathVariable("id") String serviceid) {

        System.out.println(serviceid);

        userMapper.deleteService(serviceid);

    }


    //    后台管理更新服务图片
    //
    @PostMapping("/uploadServiceImage")
    public void uploadServiceImage(MultipartFile img, String servicename) throws IOException {


//        System.out.println(img);
//        System.out.println(servicename);
        //创建oss对象
        OSS ossClient = this.ossClient.getOssClient();

        //获取上传的文件

        if (!img.isEmpty()) {
            //获取文件类型及后缀
            String[] fileType = img.getContentType().split("\\/");
            //判断file类型
            if (fileType[0].equals("image")) {
                //获取文件后缀
                String suffix = "." + fileType[1];
                //设置存储的文件名
//
                String imgFileName = new Date().getTime() + suffix;
                try {

//                      oss存放路径
                    String ossimgObject = "妙邻维修" + "/" + "后台管理服务图片" + "/" + imgFileName;
//                      将图片存入oss
                    ossClient.putObject(oss.getBUCKET_NAME(), ossimgObject, img.getInputStream());
                    //设置oss访问权限为公开读
                    ossClient.setObjectAcl(oss.getBUCKET_NAME(), ossimgObject, CannedAccessControlList.PublicRead);

                    //获取oss公开读路径
                    String imgUrl = "https://" + oss.getBUCKET_IP() + "/" + "妙邻维修" + "/" + "后台管理服务图片" + "/" + imgFileName;

                    //将url地址存入数据库
                    serviceMapper.saveImageurl(imgUrl, servicename);


                    //捕获异常
                } catch (ClientException | OSSException ce) {
                    ce.printStackTrace();
                } finally {
                    this.ossClient.getOssClient().shutdown();
                }
            } else {

            }
        } else {

        }

    }


    //获取配件列表
    @GetMapping("/getpartslist")
    public List<Part> getpartslist(Integer page, Integer pagesize) {

        System.out.println(serviceMapper.getPartslistByLimit(page, pagesize));
        return serviceMapper.getPartslistByLimit(page, pagesize);
    }

    //获取长度
    @GetMapping("/getpartslistlength")
    public Integer getpartslistlength() {

        return serviceMapper.getpartslistlength();

    }

    //添加配件

    @GetMapping("/addpart")
    public Integer addpart(String serviceid, String partname, String partprice, String category, String desc, String brand) {


        if (serviceMapper.partisExisted(partname) == 0) {
            serviceMapper.addpart(serviceid, partname, partprice, category, desc, brand);

            return 1;

        } else {
            return 0;
        }


    }

    //查询配件
    @GetMapping("/cxparts")
    public List<Part> cxparts(Integer page, Integer pagesize, String partname) {


        System.out.println(serviceMapper.cxparts(page, pagesize, partname));
        return serviceMapper.cxparts(page, pagesize, partname);

    }


    //删除配件
    @GetMapping("/deletePart/{id}")
    public void deletePart(@PathVariable("id") String serviceid) {

//        System.out.println(id);

        serviceMapper.deletePart(serviceid);

    }

    //修改配件
    @GetMapping("/updatepart")
    public int updatepart(String serviceid, String partname, String partprice, String category, String desc, String brand, String id, String oldpartname) {


        if (serviceMapper.partisExisted(partname) == 0) {
            serviceMapper.updatepart(serviceid, partname, partprice, category, desc, brand, id);
            return 1;
        }
        if (partname.equals(oldpartname)) {
            serviceMapper.updatepart(serviceid, partname, partprice, category, desc, brand, id);
            return 1;
        }
        else {
            return 0;
        }
    }


    //获取订单列表
    @GetMapping("/getorderlist")
    public List<Order> getorderlist(Integer page, Integer pagesize) {

        System.out.println(orderMapper.getorderlist(page, pagesize));
        return orderMapper.getorderlist(page, pagesize);
    }

    //获取查询的总数
    @GetMapping("/getorderlistlength")
    public Integer getorderlistlength() {

        return orderMapper.getorderlistlength();

    }


    @GetMapping("/cxorder")
    public List<Order> cxorder(Integer page, Integer pagesize, String orderid) {


        System.out.println(orderMapper.cxorder(page, pagesize, orderid));
        return orderMapper.cxorder(page, pagesize, orderid);


    }


    @GetMapping("/getswiperlist")
    public List<ServiceSwiper> getswiperlist(Integer page, Integer pagesize) {


        return orderMapper.getswiperlist(page, pagesize);


    }


    //    轮播图管理上传图片
    //
    @PostMapping("/uploadswiperImage")
    public void uploadswiperImage(MultipartFile img, String serviceid, String column) throws IOException {

        System.out.println(img);
        System.out.println(column);
        System.out.println(serviceid);
        //创建oss对象
        OSS ossClient = this.ossClient.getOssClient();

        //获取上传的文件

        if (!img.isEmpty()) {
            //获取文件类型及后缀
            String[] fileType = img.getContentType().split("\\/");
            //判断file类型
            if (fileType[0].equals("image")) {
                //获取文件后缀
                String suffix = "." + fileType[1];
                //设置存储的文件名
//
                String imgFileName = new Date().getTime() + suffix;
                try {

//                      oss存放路径
                    String ossimgObject = "妙邻维修" + "/" + "后台管理轮播图图片" + "/" + imgFileName;
//                      将图片存入oss
                    ossClient.putObject(oss.getBUCKET_NAME(), ossimgObject, img.getInputStream());
                    //设置oss访问权限为公开读
                    ossClient.setObjectAcl(oss.getBUCKET_NAME(), ossimgObject, CannedAccessControlList.PublicRead);

                    //获取oss公开读路径
                    String imgUrl = "https://" + oss.getBUCKET_IP() + "/" + "妙邻维修" + "/" + "后台管理轮播图图片" + "/" + imgFileName;

                    //将url地址存入数据库

                    serviceMapper.saveImageurl2(imgUrl, serviceid, column);


                    //捕获异常
                } catch (ClientException | OSSException ce) {
                    ce.printStackTrace();
                } finally {
                    this.ossClient.getOssClient().shutdown();
                }
            } else {

            }
        } else {

        }

    }


}